package com.edmond.jyframe.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.edmond.jyframe.R;
import com.edmond.jyframe.activity.GoodsInfoActivity;
import com.edmond.jyframe.adapter.HomeAdapter;
import com.edmond.jyframe.classes.GoodsItem;

import java.util.ArrayList;

/**
 * Created by edmond on 17-4-24.
 */

public class HomeFragment extends Fragment {

    private View view = null;
    private Activity activity = null;

    private RecyclerView recyclerView = null;

    private HomeAdapter homeAdapter = null;
    private ArrayList<GoodsItem> data = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getActivity();
        view = inflater.inflate(R.layout.fragment_home,container,false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());
        data.add(new GoodsItem());

        homeAdapter = new HomeAdapter(activity,data);

        recyclerView.setLayoutManager(new GridLayoutManager(activity,2));
        recyclerView.setAdapter(homeAdapter);

        homeAdapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, String goodsid, int position) {
                Log.d("sdfds","dd");
                Intent intent = new Intent(activity, GoodsInfoActivity.class);
                intent.putExtra("GoodsId",goodsid);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, String goodsid, int position) {
                Log.d("sdfsd","LLLLL");
            }
        });

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        activity = null;
    }
}
